// 统一支付
const app = getApp();
import siteinfo from '@/siteinfo.js';
import PayTypeEnum from '@/utils/enum/PayType.js';
import {
	isWeixin
} from '@/utils/verify.js';

module.exports = {
	/**
	 * 请求支付
	 * @param url method
	 * @param params 参数
	 * @param paySuccess 支付成功回调
	 * @param payFail	支付失败回调
	 * @param noPayment 没有生成支付数据回调
	 * @param requestSuccess 请求成功回调
	 * @param requestComplete 请求完成回调
	 */
	request(url, params, paySuccess, payFail, noPayment, requestSuccess, requestComplete) {
		app._post_form(url, params, (result) => {
				if (1 == result.code) {
					requestSuccess && requestSuccess(result.data);
				}
				let {
					pay_type,
					payment,
				} = result.data;
				if (typeof payment == 'boolean' && !payment) {
					// 没有生成支付数据
					noPayment && noPayment(result.msg.error);
					return false;
				}
				// 发起微信支付
				if (pay_type == PayTypeEnum.WECHAT) {
					// #ifdef MP-WEIXIN
					app.wxPayment({
						payment: payment,
						success: () => {
							paySuccess && paySuccess(pay_type, result.msg.success);
						},
						fail: () => {
							payFail && payFail(result.msg.error);
						}
					});
					// #endif
					// #ifdef H5
					if (!isWeixin()) {
						app.H5Payment(payment.mweb_url);
					} else {
						app.wxH5Payment({
							payment: payment,
							success: () => {
								paySuccess && paySuccess(pay_type, result.msg.success);
							},
							fail: () => {
								payFail && payFail(result.msg.error);
							}
						});
					}
					// #endif
				} else if (pay_type == PayTypeEnum.ALIPAY) {
					// 支付宝
					// #ifdef APP
					uni.requestPayment({
						provider: "alipay",
						orderInfo: payment,
						success: (res) => {
							paySuccess && paySuccess(pay_type, result.msg.success);
						},
						fail: (res) => {
							payFail && payFail(result.msg.error);
						}
					})
					// #endif
					// #ifdef H5
					app.H5Payment(payment);
					// #endif
				} else if (pay_type == PayTypeEnum.BALANCE) {
					// 余额支付
					if (1 == result.data.success) {
						paySuccess && paySuccess(pay_type, result.msg.success);
					} else {
						payFail && payFail(result.msg.error);
					}
				} else if (pay_type == PayTypeEnum.HUIPAY) {
					// 聚合支付
					let payUrl = payment.expend.pay_info;

					// 接口返回失败
					if (payment.status === 'failed') {
						payFail && payFail(payment.error_msg);
						return;
					}

					// #ifdef H5
					app.H5Payment(payUrl);
					if (!isWeixin()) {
						setTimeout(() => {
							uni.showModal({
								title: '提示',
								content: '是否已完成支付！',
								success: (res) => {
									// 默认success，可刷新订单
									paySuccess && paySuccess(pay_type, result.msg.success);
								}
							});
						}, 2000)
					}
					// #endif
				} else if (pay_type == PayTypeEnum.LL_ALIPAY || pay_type == PayTypeEnum.LL_BANK) {
					if (payment.status == 'succeeded') {
						// #ifdef H5
						app.H5Payment(payment.gateway_url);
						// #endif
						// #ifndef H5
						app.navigationTo(`${siteinfo.siteroot}ll-pay.html?url=${payment.gateway_url}`);
						// #endif
					} else {
						payFail && payFail(payment.msg);
					}
				} else if (pay_type == PayTypeEnum.BANK) {
					// #ifdef H5
					if (payment.status == 'succeeded') {
						app.H5Payment(payment.gateway_url);
					} else {
						payFail && payFail(result.msg.error);
					}
					// #endif
				} else if (pay_type == PayTypeEnum.LL_BALANCE) {
					// 连连余额
					if (payment.status == 'succeeded') {
						let ret_code = payment.ret_code;
						if ('0000' == ret_code) {
							// 支付成功
							paySuccess && paySuccess(pay_type, result.msg.success);
						} else if ('8888' == ret_code) {
							// 需要输验证码，业务逻辑在 requestSuccess 回调中处理。
						}
					} else {
						payFail && payFail(payment.msg);
					}
				} else if (pay_type == PayTypeEnum.TT_BANK) {
					// 统统付
					// #ifdef H5
					if (payment.resp_code == '000000') {
						app.H5Payment(payment.redirect_url);
					} else {
						payFail && payFail(result.msg.error);
					}
					// #endif
				} else if (pay_type == PayTypeEnum.SXY_BANK) {
					// 首信易
					if (payment.status == 'REDIRECT') {
						app.navigationTo(payment.redirectUrl);
					} else {
						payFail && payFail(result.msg.error);
					}
				}
			},
			null, () => {
				requestComplete && requestComplete();
			}
		);
	},

	/**
	 * 连连余额支付二次验证
	 * @param {Object} verify_code 连连支付发送的验证码
	 * @param {Object} params 参数
	 * @param {Object} success 支付成功回调
	 */
	requestLLBalancePay(verify_code, params, success, fail) {
		app._post_form('user.dealer.withdraw/validationSms', {
			data: JSON.stringify(Object.assign({}, params, {
				verify_code: verify_code,
				type: 'balance_pay'
			}))
		}, (res) => {
			success && success(res);
		}, () => {
			fail && fail();
		});
	}
}
